//
//  main.m
//  Demo0014
//
//  Created by zTercel on 13-1-14.
//  Copyright (c) 2013年 zTercel. All rights reserved.
//

#import <Foundation/Foundation.h>

// Creating an Array
//--------------------------------------------
// + array
// + arrayWithArray:
// + arrayWithObject:
// + arrayWithObjects:
// + arrayWithObjects:count:

//Initializing an Array
//--------------------------------------------
// – initWithArray:
// – initWithObjects:
// – initWithObjects:count:
void createArray()
{
     NSLog(@"=============Creating and Initlizing an array=============");
    // arrayWithArray 
    {
        NSNumber* intArray[]    = {[NSNumber numberWithUnsignedInt:123], [NSNumber numberWithUnsignedInt:234], [NSNumber numberWithUnsignedInt:345], [NSNumber numberWithUnsignedInt:456]};
        NSArray* array          = [NSArray arrayWithObjects:intArray count:sizeof(intArray)/sizeof(*intArray)];
        NSArray* array1         = [[NSArray alloc] initWithArray: array];
    
        for (int ii = 0; ii < [array1 count]; ii++) {
            NSLog(@"array[%d] = %@", ii, [array1 objectAtIndex:ii]);
        }  
    }
    
    // arrayWithObject
    {
        NSNumber* IntNumber = [NSNumber numberWithInteger:1000];
        NSArray* array      = [NSArray arrayWithObject:IntNumber];
        NSLog(@"array[0] = %@", [array objectAtIndex:0]);
    }
    
    // arrayWithObjects
    {
        NSNumber* charNumber            = [NSNumber numberWithChar:'9'];
        NSString* string                = @"this is a string";
        NSMutableString* mutableString  = [NSMutableString stringWithFormat: @"%@", string];
        [mutableString replaceCharactersInRange:NSMakeRange([string length] - 6, 6) withString:@"mutable string"];
        NSArray* array                  = [NSArray arrayWithObjects: charNumber, string, mutableString, nil];
        for (int ii = 0; ii < [array count]; ii++)
        {
            NSLog(@"array[%d] = %@", ii, [array objectAtIndex:ii]);
        }
    }
    
    // arrayWithObjects:count
    {
        NSNumber* intArray[] = {[NSNumber numberWithUnsignedInt:123], [NSNumber numberWithUnsignedInt:234], [NSNumber numberWithUnsignedInt:345], [NSNumber numberWithUnsignedInt:456]};
        NSArray* array  = [NSArray arrayWithObjects:intArray count:sizeof(intArray)/sizeof(*intArray)];
        int ii = 0;
        for (NSArray* element in array) {
            NSLog(@"array[%d] = %@", ii++, element);
        }
    }
    
    return;
}

// Querying an Array
//-------------------------------
// – containsObject:
// – count
// – getObjects:range:
// – lastObject
// – objectAtIndex:
void queryArray()
{
    NSLog(@"=============Querying an array===============");
    NSString* stringArray[] = {@"I", @"am", @"a", @"boy"};
    NSArray* array  = [NSArray arrayWithObjects:stringArray count:sizeof(stringArray)/sizeof(*stringArray)];
    NSLog(@"array content: %@", [array description]);
    NSLog(@"array containObject: am?  %s", [array containsObject:@"am"] ? "YES" : "NO");
    
    NSLog(@"array count: %ld", [array count]);
    NSLog(@"array[1] = %@", [array objectAtIndex:1]);
    NSLog(@"last Object = %@", [array lastObject]);
  
    return;
}

void sortArray()
{
    NSMutableArray* array = [NSMutableArray arrayWithObjects: @"Monday", @"Tuesday", @"Wedesday", @"Thursday", @"Friday", @"Saturday", @"Sunday", nil];
    
    NSLog(@"%@", [array description]);
    [array sortUsingComparator:
        ^(id obj1, id obj2)
        {
        return [obj1 compare:obj2];
        }];
    
    NSLog(@"%@", [array description]);
    
}

int main(int argc, const char * argv[])
{
    @autoreleasepool {
        createArray();
        queryArray();
        sortArray();
    }
    return 0;
}

